package com.aspire.moss.portal.desktop;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Set;
import java.util.Map.Entry;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;

import com.aspire.moss.portal.R;
import com.aspire.moss.portal.desktop.AppView.OnDeleteClickListener;

public class WorkspaceAdapter extends Observable {

	private static final String TAG = "WorkspaceAdapter";

	private Context mContext;

	private HashMap<Integer, LinkedList<AppInfo>> apps;

	private OnClickListener onClickListener;

	private OnDeleteClickListener onDeleteClickListener;

	private OnLongClickListener onLongClickListener;

	public WorkspaceAdapter(Context mContext,
			HashMap<Integer, LinkedList<AppInfo>> apps) {
		this.mContext = mContext;
		this.apps = apps;
	}

	public HashMap<Integer, LinkedList<AppInfo>> getApps() {
		return apps;
	}

	public void setApps(HashMap<Integer, LinkedList<AppInfo>> apps) {
		this.apps = apps;
	}

	public View getGroupView(ViewGroup parment, int index) {
		return Utils.getLayoutInflater(mContext).inflate(R.layout.celllayou,
				null);
	}

	public AppView getChildView(int group, int index) {
		Log.d(TAG, "getChildView group=" + group + " index=" + index);
		final AppInfo info = apps.get(group).get(index);
		final AppView view = (AppView) Utils.getLayoutInflater(mContext)
				.inflate(R.layout.app, null);
		view.appInfo = info;
		if (null != info.iconBitmap) {
//			((ImageView) view.findViewById(R.id.icon))
//					.setImageBitmap(info.iconBitmap);
			view.setIcon(info.iconBitmap , false);
		}
//		final TextView textView = ((TextView) view.findViewById(R.id.title));
		view.setText(info.title);
		if (null != onClickListener)
			view.setOnClickListener(onClickListener);
		if (null != onLongClickListener)
			view.setOnLongClickListener(onLongClickListener);
		if (null != onDeleteClickListener) {
			view.setOnDeleteClickListener(onDeleteClickListener);
		}
		view.postInvalidate();
		return view;
	}

	/**
	 * 得到有多少屏幕
	 * 
	 * @return
	 */
	public int getCount() {
		return apps.size();
	}

	/**
	 * 添加 一个屏幕
	 * 
	 * @param screenId
	 */
	public void addGroupView(int screenId) {
		if (null != apps) {
			apps.put(screenId, new LinkedList<AppInfo>());
		}
	}

	public LinkedList<AppInfo> getGroup(int screenId) {
		final LinkedList<AppInfo> group = apps.get(screenId);
		return group != null ? group : null;
	}

	/**
	 * 通知
	 */
	public void notifyObservers() {
		setChanged();
		notifyObservers(null);
	}

	/**
	 * 删除一个应用的数据
	 * 
	 * @param appInfo
	 */
	public void removeAppInfo(AppInfo appInfo) {
		if (null != appInfo) {
			final int screenId = appInfo.screen;
			final LinkedList<AppInfo> group = getGroup(screenId);
			if (null != group) {
				for (final AppInfo info : group) {
					if (info.eqXYScreen(appInfo)) {
						group.remove(info);//删除在数据源里的数据
						Utils.deleteAppInfo(mContext, appInfo);//删除保存在SharedPreferences里的数据
						return;
					}
				}
			}
		}
	}

	public void printInfo() {
		if (null != apps) {
			final Set<Entry<Integer, LinkedList<AppInfo>>> sets = apps
					.entrySet();
			LinkedList<AppInfo> aps;
			Integer integer = null;
			for (Entry<Integer, LinkedList<AppInfo>> entry : sets) {
				integer = entry.getKey();
				if (null != integer) {
					aps = apps.get(integer);
					for (AppInfo info : aps) {
						Log.i(TAG, "info:" + info);
					}
				}
			}
		}

	}

	public OnClickListener getOnClickListener() {
		return onClickListener;
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}

	public OnDeleteClickListener getOnDeleteClickListener() {
		return onDeleteClickListener;
	}

	public void setOnDeleteClickListener(
			OnDeleteClickListener onDeleteClickListener) {
		this.onDeleteClickListener = onDeleteClickListener;
	}

	public OnLongClickListener getOnLongClickListener() {
		return onLongClickListener;
	}

	public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
		this.onLongClickListener = onLongClickListener;
	}
}
